package com.example.job.handle;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.core.constants.CacheConstants;
import com.example.job.entity.DO.ExamDO;
import com.example.job.enums.ExamStatus;
import com.example.job.mapper.ExamMapper;
import com.example.redis.service.RedisService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Component
@Slf4j
public class ExamXxlJob {

    @Autowired
    private ExamMapper examMapper;

    @Autowired
    private RedisService redisService;


    /**
     * <p>统计哪些竞赛应该存入未完赛的列表中 </p>
     * <p>哪些竞赛应该存入历史竞赛列表中</p>
     * <p>统计出来了之后，再存入对应的缓存中</p>
     *
     */
    @XxlJob("examListOrganizeHandler")
    public void examListOrganizeHandler() {
        log.info("*** examListOrganizeHandler ***");
        List<ExamDO> unFinishList = examMapper.selectList(new LambdaQueryWrapper<ExamDO>()
                .select(ExamDO::getExamId, ExamDO::getTitle, ExamDO::getStartTime, ExamDO::getEndTime)
                .gt(ExamDO::getEndTime, LocalDateTime.now())
                .eq(ExamDO::getStatus, ExamStatus.PUBLISHED.getStatus())
                .orderByDesc(ExamDO::getCreateTime));
        refreshCache(unFinishList, CacheConstants.EXAM_UNFINISHED_LIST);

        List<ExamDO> historyList = examMapper.selectList(new LambdaQueryWrapper<ExamDO>()
                .select(ExamDO::getExamId, ExamDO::getTitle, ExamDO::getStartTime, ExamDO::getEndTime)
                .le(ExamDO::getEndTime, LocalDateTime.now())
                .eq(ExamDO::getStatus, ExamStatus.PUBLISHED.getStatus())
                .orderByDesc(ExamDO::getCreateTime));

        refreshCache(historyList, CacheConstants.EXAM_HISTORY_LIST);
        log.info("*** examListOrganizeHandler 统计结束 ***");
    }


    public void refreshCache(List<ExamDO> examList, String examListKey) {
        if (CollectionUtil.isEmpty(examList)) {
            return;
        }

        Map<String, ExamDO> examMap = new HashMap<>();
        List<Long> examIdList = new ArrayList<>();
        for (ExamDO exam : examList) {
            examMap.put(getDetailKey(exam.getExamId()), exam);
            examIdList.add(exam.getExamId());
        }
        redisService.multiSet(examMap);  //刷新详情缓存
        redisService.deleteObject(examListKey);
        redisService.rightPushAll(examListKey, examIdList);      //刷新列表缓存
    }

    private String getDetailKey(Long examId) {
        return CacheConstants.EXAM_DETAIL + examId;
    }

    private String getUserMsgListKey(Long userId) {
        return CacheConstants.USER_MESSAGE_LIST + userId;
    }

    private String getMsgDetailKey(Long textId) {
        return CacheConstants.MESSAGE_DETAIL + textId;
    }

    private String getExamRankListKey(Long examId) {
        return CacheConstants.EXAM_RANK_LIST + examId;
    }
}
